package com.cxb.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cxb.R;

/**
 * ˵��:FMƵ��ѡ����
 * 
 * @author:��ΰ��
 *             2013-1-10
 */
/**
 * @{# FMProgressView.java Create on 2013-2-21 ����8:29:31
 * 
 *     �๦��˵��:
 * @Version 1.0
 * @Author ��ΰ��
 * 
 * 
 */
public class FMProgressView extends LinearLayout implements OnClickListener {

	private ImageView slideBar;
	private ImageButton ibFmAdd;
	private ImageButton ibFmDec;
	private ImageView ivProgressBar;
	private float accuracy = 0.5f;// ����
	public static float range_less = 89f;// fm���Ƶ��
	private float range_max = 108.0f;// fm���Ƶ��
	private float progress = 0;// ��ǰ���
	private float kaola_fm = 94.3f;// 为考拉FM预留一个频道
	private OnFmProgressChangeListener onFmProgressChangeListener;
	private Context mContext;

	public FMProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		init();

	}

	// 87.5,108.0;
	public FMProgressView(Context context) {
		super(context);
		this.mContext = context;
		init();

	}

	private void init() {
		LayoutInflater.from(mContext).inflate(R.layout.fm_progress_bar, this);

		slideBar = (ImageView) this.findViewById(R.id.slideBar);
		ibFmAdd = (ImageButton) this.findViewById(R.id.fm_add);
		ibFmDec = (ImageButton) this.findViewById(R.id.fm_dec);
		ivProgressBar = (ImageView) this.findViewById(R.id.progress_bar);

		ibFmAdd.setOnClickListener(this);
		ibFmDec.setOnClickListener(this);

	}

	public void setOnFmProgressChangeListener(
			OnFmProgressChangeListener onFmProgressChangeListener) {
		this.onFmProgressChangeListener = onFmProgressChangeListener;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {

		super.onLayout(changed, l, t, r, b);
		calculateSlideBarPosition();
	}

	/**
	 * ˵��:���㻬��λ�� Author:��ΰ�� 2013-1-10
	 */
	private void calculateSlideBarPosition() {

		// float preProgress;
		// float a=progress+range_less;
		// if((progress+range_less)<kaola_fm&&(progress+range_less+accuracy)>kaola_fm){
		// // preProgress=progress;
		// progress=kaola_fm-range_less;
		// }
		int top = ivProgressBar.getHeight() / 2 - 2;
		float max_left = 290;
		float less_left = 10;
		int left = (int) (ibFmAdd.getWidth() + less_left + ((max_left - less_left) / (range_max - range_less))
				* (progress));// 28// 210
		slideBar.layout(left, top, left + slideBar.getWidth(),
				top + slideBar.getHeight());

		if (onFmProgressChangeListener != null) {
			onFmProgressChangeListener
					.onFmProgressChange(progress + range_less);
		}
		// preProgress=progress;
	}

	@Override
	public void onClick(View v) {

		if (v.getId() == ibFmAdd.getId()) {// ����

			if (progress > 0) {
				if (progress + range_less == 94.5) {
					progress = kaola_fm - range_less;
				} else {
					if (progress + range_less == kaola_fm) {
						progress = 94.5f - range_less;
					}
					progress -= accuracy;
				}
				calculateSlideBarPosition();
			}
		} else if (v.getId() == ibFmDec.getId()) {// ����

			if (progress < range_max - range_less) {
				if (progress + range_less == 94) {
					progress = kaola_fm - range_less;
				} else {
					if (progress + range_less == kaola_fm) {
						progress = 94f - range_less;
					}
					progress += accuracy;
				}
				calculateSlideBarPosition();
			}
		}
	}

	public float getProgress() {
		return progress;
	}

	public void setProgress(float progress) {
		this.progress = progress;
	}

	public float getChannel() {
		return progress + range_less;
	}

	public void setChannel(float progress) {
		this.progress = progress - range_less;
	}

	public interface OnFmProgressChangeListener {
		void onFmProgressChange(float progress);
	}

	public float getRange_less() {
		return range_less;
	}

	public void setRange_less(float range_less) {
		this.range_less = range_less;
	}

}
